// import axios from  'axios'
// import qs from 'qs'
// import store from "./store";
//
// let  http=axios.create({
//   baseURL:'http://localhost:7000',
//   method:'POST',
//   //responseType: 'application/json'
//   //携带凭证
//   //withCredentials:true,
// })
//
// //请求拦截器
// http.interceptors.request.use(requestConfig=>{
//   if (requestConfig.method=='post'){
//    requestConfig.headers['Context-Type']="application/x-www-form-urlencoded"
//     requestConfig.data=qs.stringify(requestConfig.data);
//   }
//   //登陆成功之后，要携带token值
//   let token=sessionStorage.getItem('token')
//    console.log(token+"6666")
// if (token!=null){
//   //请求头中携带token信息
// token=qs.parse(token)
//
// requestConfig.headers={
//   'Context-Type': "application/x-www-form-urlencoded",
//   'Authorization':token.token_type + ' ' + token.access_token
// }
// }
//   return requestConfig
// })
//
// //拦截器
// http.interceptors.response.use(response => {
//   return response.data
// }, error => {
//   console.log("响应过期失败")
//   let response = error.response;
//   let a=response.data.error_description;
//   console.log("报错信息："+a)
//   // 判断失败的原因是token过期：status=401+"error_description": "Access token expired:
//   if (response.status == 401&&response.data.error_description.startsWith('Access token expired')) {
//     console.log(store.state.client.client_id)
//     console.log(store.state.client.client_secret)
//     let refresh_token = qs.parse(sessionStorage.getItem('token')).refresh_token;
//     console.log(refresh_token)
//     // 刷新token,获取新的token值
//     let responseResult=http.post('bookserver/oauth/token', {
//       // 访问刷新token接口，获取新的token
//       grant_type: 'refresh_token',
//       client_id: store.state.client.client_id,
//       client_secret: store.state.client.client_secret,
//       refresh_token: response.data.refresh_token
//     }).then(data =>{// 获取新的token
//       console.log(data)
//       console.log("更新token")
//       // 更新token
//       sessionStorage.setItem('token',qs.stringify(data));
//       console.log(error)
//       return error.config; // 前一次错误请求的配置信息
//     }).then(config => {
//       console.log(config)
//       return http(config); // 重新发送请求
//     })
//     return responseResult;
//   }
//   return Promise.reject(error);
// })
//
// export default http
//
//
//
//

import axios from 'axios'
import qs from 'qs'
import store from "./store";
import {data} from "autoprefixer";
import {config} from "shelljs";
import {promise} from "ora";
import da from "element-ui/src/locale/lang/da";

let http = axios.create({
  baseURL: 'http://localhost:7000/frontService',
  method: 'POST',
  //携带凭证
  withCredentials: true

})

//请求拦截器
http.interceptors.request.use(requestConfig => {

  if (requestConfig.method == 'post') {
    requestConfig.headers['Context-Type'] = "application/x-www-form-urlencoded"
    requestConfig.data = qs.stringify(requestConfig.data);
  }
  //获取token数据
  let token = sessionStorage.getItem("token")
  console.log(token)
  if (token != null) {
    token = qs.parse(token);
    requestConfig.headers = {
      'Content-Type': "application/x-www-form-urlencoded",
      'Authorization': token.token_type + ' ' + token.access_token
    }
  }
  return requestConfig

})


//拦截然后响应

http.interceptors.response.use(response => {
  return response.data
},error => {
  console.log("报错信息1："+error.response.data.error_description)
  let response=error.response
  //判断是令牌过期
  if (response.status == 401 && response.data.error_description.startsWith('Access token expired')){
    console.log("token过期")
    let data=qs.parse(sessionStorage.getItem("token"));
    console.log("哈哈哈"+store.state.client.client_id)
    //进入接口，用刷新码,获取新的token
    let responseResult=http.post("http://localhost:7000/authserver/oauth/token",{
      grant_type: 'refresh_token',
      client_id: store.state.client.client_id,
      client_secret: store.state.client.client_secret,
      refresh_token: data.refresh_token
    }).then(data=>{
      console.log("11111111"+data)
      //把值更新
      sessionStorage.setItem("token",qs.stringify(data.data))
      return error.config
    }).then(config=>{
      //重新发送请求
      return http(config)
    })
    return responseResult
  }
  return Promise.reject(error)
})

export default http
